package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.constant.CardReturnConstants;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.core.utils.helper.StringHelper;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.domain.Returncard;
import com.ktgj.customer.service.ILoginService;
import com.ktgj.customer.service.IReturncardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 退卡申请Controller
 * 
 * @author ruoyi
 * @date 2021-09-15
 */
@ControllerAdvice
@RestController
@RequestMapping("/returncard")
public class ReturncardController extends BaseController
{
    @Autowired
    private IReturncardService returncardService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询退卡申请列表
     */
    //@PreAuthorize(hasPermi = "system:returncard:list")
    @GetMapping("/list")
    public TableDataInfo list(Returncard returncard)
    {
        startPage();
        List<Returncard> list = returncardService.selectReturncardList(returncard);
        return getDataTable(list);
    }

    /**
     * 导出退卡申请列表
     */
    //@PreAuthorize(hasPermi = "system:returncard:export")
    @Log(title = "退卡申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Returncard returncard) throws IOException
    {
        List<Returncard> list = returncardService.selectReturncardList(returncard);
        ExcelUtil<Returncard> util = new ExcelUtil<Returncard>(Returncard.class);
        util.exportExcel(response, list, "退卡申请数据");
    }

    /**
     * 获取退卡申请详细信息
     */
    //@PreAuthorize(hasPermi = "system:returncard:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(returncardService.selectReturncardById(orderId));
    }

    /**
     * 新增退卡申请
     */
    //@PreAuthorize(hasPermi = "system:returncard:add")
    @Log(title = "退卡申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Returncard returncard)
    {
        return toAjax(returncardService.insertReturncard(returncard));
    }

    /**
     * 修改退卡申请
     */
    //@PreAuthorize(hasPermi = "system:returncard:edit")
    @Log(title = "退卡申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Returncard returncard)
    {
        return toAjax(returncardService.updateReturncard(returncard));
    }

    /**
     * 删除退卡申请
     */
    //@PreAuthorize(hasPermi = "system:returncard:remove")
    @Log(title = "退卡申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(returncardService.deleteReturncardByIds(orderIds));
    }

    //  @PostMapping("")

    /**
     * 申请退卡
     * @param request
     * @param returncard
     * @return
     */
    @PostMapping("applyRefund")
    public AjaxResult applyRefund(HttpServletRequest request, @RequestBody Returncard returncard) {
        Employee employee = loginService.getLoginEmployee(request);
        returncard.setCreatepersonid(employee.getEmployeeId());
        returncard.setState(CustomerConstants.applyRefund.getValue());
        return toAjax(returncardService.applyRefund(returncard));
    }

    /**
     * 退卡申请通过
     * @return
     */
    @PostMapping("refundApproved")
    public AjaxResult refundApproved(@RequestBody Map<String, Object> map){
        if (map.get("returnCardId") != null && !map.get("returnCardId").equals("")){
            Long returnCardId = Long.valueOf(map.get("returnCardId").toString());
            int i;
            try {
                i = returncardService.refundApproved(returnCardId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("成功");
            }
            else {
                return AjaxResult.error("失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 退卡申请未通过
     * @return
     */
    @PostMapping("refuseRefund")
    public AjaxResult refuseRefund(@RequestBody Map<String, Object> map){
        return toAjax(returncardService.refuseRefund(new Long(map.get("returnCardId").toString())));
    }

    //crm退卡页面基本信息获取
    @PostMapping("queryRefuseCardData")
    public AjaxResult queryRefuseCardData(@RequestBody Map<String, Object> map)
    {
        Long customerId = null;
        if (map.get("customerId") != null)
            customerId = new Long(map.get("customerId").toString());
        String cardCode = null;
        if (map.get("cardCode") != null)
            cardCode = map.get("cardCode").toString();

        if (!StringHelper.isNullOrEmpty(cardCode))
        {
            Long cardId = Long.valueOf(map.get("cardId").toString());
            return AjaxResult.success(returncardService.queryRefuseCardData(customerId, cardCode, cardId));
        }
        return AjaxResult.error("查询卡信息失败！");
    }
}
